/*
 * $Id: Precondition.java,v 1.1 2001/09/23 10:33:46 jppietsch Exp $
 * Copyright (C) 2001 Jan-Peter Pietsch
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.jppietsch.error;


/**
 * Convenience methods to assert preconditions of methods.
 *
 * @author Jan-Peter Pietsch
 * @version $Revision: 1.1 $
 */
public class Precondition {

    public static void argumentValid(String aName,
                                     Object aValue,
                                     boolean aCondition) {
        if (!aCondition) {
            fail(aName, aValue);
        }
    }

    public static void argumentValid(String aName,
                                     long aValue,
                                     boolean aCondition) {
        if (!aCondition) {
            fail(aName, aValue);
        }
    }

    public static void argumentNotNegative(String aName, long aValue) {
        argumentValid(aName, new Long(aValue), aValue >= 0);
    }

    public static void argumentNotNull(String aName, Object aValue) {
        argumentValid(aName, aValue, aValue != null);
    }

    public static void argumentEquals(String aName,
                                      long anExpectedValue,
                                      long anActualValue) {
        argumentValid(aName, new Long(anActualValue),
                anExpectedValue == anActualValue);
    }

    public static void argumentElementsNotNull(String aName,
                                               Object[] anArray) {
        argumentNotNull(aName, anArray);
        for (int i = 0; i < anArray.length; i++) {
            argumentNotNull(aName + "[" + i + "]", anArray[i]);
        }
    }

    public static void argumentStringNotEmpty(String aName, String aValue) {
        argumentNotNull(aName, aValue);
        argumentValid(aName, aValue, aValue.trim().length() > 0);
    }

    private static void fail(String aName, Object aValue) {
        throw new IllegalArgumentException(aName + " == " + aValue);
    }

    private static void fail(String aName, long aValue) {
        throw new IllegalArgumentException(aName + " == " + aValue);
    }

}